// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create a tent respawn point
// -----------------------------------------------

private ["_position","_mydir","_exit","_text"];

if (((getPosATL player) distance (getMarkerPos "respawn_west")) < 300) exitWith {
	wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGETRYTOMOVEOUT", localize "STR_WC_MESSAGECANCREATETENT", 10] spawn WC_fnc_playerhint;
};

_exit = false;
if (wcwithACE == 0) then {
	if (isNull (unitBackpack player)) then {
		wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGETAKEABACKPACK", localize "STR_WC_MESSAGECANCREATETENT", 10] spawn WC_fnc_playerhint;
		_exit = true;
	};
} else {
	if !(player call ace_sys_ruck_fnc_hasRuck) exitWith {
		wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGETAKEABACKPACK", localize "STR_WC_MESSAGECANCREATETENT", 10] spawn WC_fnc_playerhint;
		_exit = true;
	};
};

if (_exit) exitWith {};

_mydir = getDir player;
_position =  [(getPosATL player select 0) + (sin _mydir * 2), (getPosATL player select 1) + (cos _mydir * 2), 0];

//A3 TODO: Tents; setVehicleInit
if (isNil "wctent") then {
	wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGEBUILDING", localize "STR_WC_MESSAGEBUILDINGINFORMATION", 8] spawn WC_fnc_playerhint;
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 8;
	if !(alive player) exitWith {}; 
	wctent = createVehicle ["ACamp_EP1", _position, [], 0, "NONE"];
	wctent setPosATL _position;
	wctent setVariable ["owner", name player, true];
	_text = format ["this addAction [""<t color='#ffcb00'>Remove tent of %1</t>"", ""warcontext\actions\WC_fnc_doremovetent.sqf"", [], 6, false];", name player];
	wctent setVehicleInit _text;
	processInitCommands;
	wcrespawnposition = [getPosATL player, wctent];
	wcrespawnmarker setMarkerPosLocal _position;
	wcrespawnmarker setMarkerSizeLocal [0.5,0.5];
	wcrespawnmarker setMarkerTextLocal format ["%1 Camp", name player];
	wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGEBUILDINGFINISHED",localize "STR_WC_MESSAGEBUILDINGINFORMATION", 3] spawn WC_fnc_playerhint;
} else {
	if ((getDammage wctent > 0.9) or {!(alive wctent)}) then {
		deleteVehicle wctent;
		wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGEBUILDING", localize "STR_WC_MESSAGEBUILDINGINFORMATION", 8] spawn WC_fnc_playerhint;
		player playMove "AinvPknlMstpSlayWrflDnon_medic";
		sleep 8;
		if !(alive player) exitWith {}; 
		wctent = nil;
		wctent = createVehicle ["ACamp_EP1", _position, [], 0, "NONE"];
		wctent setPosATL _position;
		wctent setVariable ["owner", name player, true];
		_text = format ["this addAction [""<t color='#ffcb00'>Remove tent of %1</t>"", ""warcontext\actions\WC_fnc_doremovetent.sqf"", [], 6, false];", name player];
		wctent setVehicleInit _text;
		processInitCommands;
		wcrespawnposition = [getPosATL player, wctent];
		wcrespawnmarker setMarkerPosLocal _position;
		wcrespawnmarker setMarkerSizeLocal [0.5,0.5];
		wcrespawnmarker setMarkerTextLocal format ["%1 Camp", name player];
		wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGEBUILDINGFINISHED",localize "STR_WC_MESSAGEBUILDINGINFORMATION", 3] spawn WC_fnc_playerhint;
	} else {
		wcgarbage = [localize "STR_WC_MESSAGEDEPLOYATENT", localize "STR_WC_MESSAGEREMOVETENT", localize "STR_WC_MESSAGECANBUILDTENT", 10] spawn WC_fnc_playerhint;
	};
};

waitUntil {!alive wctent};

"respawn" setMarkerPosLocal [0,0,0];
"respawn" setMarkerSizeLocal [0,0]; 